package com.intellagric.service.traceability.Impl.trace_record_table;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.TableDao;
import com.intellagric.service.traceability.trace_record_table.TraceTableService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2019/2/22
 * @DESC： 溯源记录表数据操作服务
 */
@Service
public class TraceTableServiceImpl implements TraceTableService {

    @Autowired
    private TableDao tableDao;

    /**
     * 添加数据
     * @param tableName
     * @param list
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage insertTraceTable(String tableName, List<String> list) {
        if (tableDao.executeInsertSql(tableName,list) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除数据
     * @param id
     * @param tableName
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteTraceTable(String id, String tableName) {
        if (tableDao.executeDeleteSql(id,tableName) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改数据
     * @param sql
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage updateTraceTable(String sql) {
        if (tableDao.executeUpdateSql(sql) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 通过id查询数据
     * @param id
     * @param tableName
     * @return ResponseMessage
     */
    @Override
    public LayuiDataGridResult selectOneTraceTable(String id, String tableName) {
        List<LinkedHashMap<String, Object>> list = tableDao.executeSelectOneSql(id, tableName);
        return LayuiDataGridResult.success().add(list,list.size());
    }

    /**
     * 查询数据列表
     * @param tableName
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult selectTraceTable(int page,int limit,String tableName,String produceBatchId,String processId) {
        PageHelper.startPage(page,limit);
        List<LinkedHashMap<String, Object>> list = tableDao.executeSelectSql(tableName, produceBatchId, processId);
        PageInfo<LinkedHashMap<String, Object>> pageInfo = new PageInfo<>(list);
        return LayuiDataGridResult.success().add(list,pageInfo.getTotal());
    }

}
